﻿//
// Created by work on 24-6-6.
//

#ifndef IOT_PLAT_CRTDEVICEINFO_H
#define IOT_PLAT_CRTDEVICEINFO_H

#include <shlib/CValue.h>


class CRtDeviceInfo {
public:
    enum ECause{
        ETest,
        ECollect,
        EDemonstration
    };

    enum EDeviceCol {
        EUser = 0x00,
        EName,
        EModelName,
        EDescr,
        ESn,
        EGroupIngs,
        EConfigs,
        ELocation,
        ECreate_time,
        EProtocol,
        EFirmwareVersion,
        ELastOnlineTime,
        EAccruedOnlineTime,
        ESpecifications,
        EValid,

        EPulse,
        ELoginTime,
        EOnlineState
    };

    QString user;//所属用户
    QString name;//设备名字
    QString model_name;//所属模型
    QString descr;//描述
    int sn;//排序
    QString grouping;//分组
    QJsonObject configs;//配置
    QString location;//安装位置
    QDateTime create_time;//创建时间
    QString protocol;//通讯协议
    QString firmware_version;//版本
    QDateTime last_online_time;//最后更新时间
    int accrued_online_time;//累计在线时长
    QString specifications;//规格
    bool valid;//是否启用

    int pulse;
    QDateTime login_time;
    bool online_state;

    static std::map<CRtDeviceInfo::EDeviceCol, QString> rtDeviceColToStringMap;

    QJsonObject toJson();

    void fromJson(QJsonObject json);

public:
    explicit CRtDeviceInfo();

private:
};

static const QString t_device_name = "t_iot_device";
#define DEVICE_ENUM                  CRtDeviceInfo::EDeviceCol
#define deviceFieldName(type)        CRtDeviceInfo::rtDeviceColToStringMap.find(type)->second

struct SRtDeviceYxInfo {
    int v = 0;
    QDateTime ts;
    QString v_descr;
    int sn = 0;
    CRtDeviceInfo::ECause cause = CRtDeviceInfo::ECollect;

    SRtDeviceYxInfo() = default;
    SRtDeviceYxInfo(int pv, int psn, QString pv_descr, QDateTime pts, CRtDeviceInfo::ECause pcause) {
        v = pv;
        ts = pts;
        v_descr = std::move(pv_descr);
        sn = psn;
        cause = pcause;
    }
    QJsonObject toJson() {
        QJsonObject json;
        json["v"] = v;
        json["ts"] = ts.toString();
        json["sn"] = sn;
        json["v_descr"] = v_descr;
        json["cause"] = (int)cause;
        return json;
    }
};

struct SRtDeviceYcInfo {
    float v = 0;
    QDateTime ts;
    int alarm_count = 0;
    CRtDeviceInfo::ECause cause = CRtDeviceInfo::ECollect;
    bool alarm = false;
    QDateTime limit_time;

    SRtDeviceYcInfo() = default;
    SRtDeviceYcInfo(float pv, QDateTime pts, int palarm_count, CRtDeviceInfo::ECause pcause, bool palarm, QDateTime plimit_time) {
        v = pv;
        ts = pts;
        alarm_count = palarm_count;
        cause = pcause;
        alarm = palarm;
        limit_time = plimit_time;
    }
    QJsonObject toJson() {
        QJsonObject json;
        json["v"] = v;
        json["ts"] = ts.toString();
        json["alarm_count"] = alarm_count;
        json["cause"] = (int)cause;
        json["alarm"] = alarm;
        json["limit_time"] = limit_time.toString();
        json["cause"] = (int)cause;
        return json;
    }
};

struct SRtDeviceYkInfo {
    int v = 0;
    QDateTime ts;
    QString v_descr;
    CRtDeviceInfo::ECause cause = CRtDeviceInfo::ECollect;
    int sn = 0;
    SRtDeviceYkInfo() = default;
    SRtDeviceYkInfo(int pv, int psn, QString pv_descr, QDateTime pts, CRtDeviceInfo::ECause pcause) {
        v = pv;
        ts = pts;
        v_descr = std::move(pv_descr);
        sn = psn;
        cause = pcause;
    }
    QJsonObject toJson() {
        QJsonObject json;
        json["v"] = v;
        json["ts"] = ts.toString();
        json["sn"] = sn;
        json["v_descr"] = v_descr;
        json["cause"] = (int)cause;
        return json;
    }
};

struct SRtDeviceParameterInfo {
    CValue v;
    QDateTime ts;
    CRtDeviceInfo::ECause cause = CRtDeviceInfo::ECollect;

    SRtDeviceParameterInfo() = default;
    SRtDeviceParameterInfo(CValue pv, QDateTime pts, CRtDeviceInfo::ECause pcause) {
        v = std::move(pv);
        ts = pts;
        cause = pcause;
    }
    QJsonObject toJson() {
        QJsonObject json;

        switch (v.type()) {
        case CValue::IOT_Int:
            json["v"] = v.asInt();
            break;
        case CValue::IOT_String:
            json["v"] = v.asString();
            break;
        case CValue::IOT_DateTime:
            json["v"] = v.asDateTime().toString();
            break;
        default:
            json["v"] = "0";
            break;
        }

        json["ts"] = ts.toString();
        json["cause"] = (int)cause;
        return json;
    }
};

#endif //IOT_PLAT_CRTDEVICEINFO_H
